#Region "File Description"
'-----------------------------------------------------------------------------
' MenuEntry.cs
'
' XNA Community Game Platform
' Copyright (C) Microsoft Corporation. All rights reserved.
'-----------------------------------------------------------------------------
#End Region

#Region "Using Statements"
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics

#End Region

''' <summary>
''' Helper class represents a single entry in a MenuScreen. By default this
''' just draws the entry text string, but it can be customized to display menu
''' entries in different ways. This also provides an event that will be raised
''' when the menu entry is selected.
''' </summary>
Class MenuEntry
	#Region "Fields"

	''' <summary>
	''' The text rendered for this entry.
	''' </summary>
	Private m_text As String

	''' <summary>
	''' Tracks a fading selection effect on the entry.
	''' </summary>
	''' <remarks>
	''' The entries transition out of the selection effect when they are deselected.
	''' </remarks>
	Private selectionFade As Single

	''' <summary>
	''' The position at which the entry is drawn. This is set by the MenuScreen
	''' each frame in Update.
	''' </summary>
	Private m_position As Vector2

	#End Region

	#Region "Properties"


	''' <summary>
	''' Gets or sets the text of this menu entry.
	''' </summary>
	Public Property Text() As String
		Get
			Return m_text
		End Get
		Set
			m_text = value
		End Set
	End Property


	''' <summary>
	''' Gets or sets the position at which to draw this menu entry.
	''' </summary>
	Public Property Position() As Vector2
		Get
			Return m_position
		End Get
		Set
			m_position = value
		End Set
	End Property


	#End Region

	#Region "Events"


	''' <summary>
	''' Event raised when the menu entry is selected.
	''' </summary>
	Public Event Selected As EventHandler(Of PlayerIndexEventArgs)


	''' <summary>
	''' Method for raising the Selected event.
	''' </summary>
	Protected Friend Overridable Sub OnSelectEntry(playerIndex As PlayerIndex)
		RaiseEvent Selected(Me, New PlayerIndexEventArgs(playerIndex))
	End Sub


	#End Region

	#Region "Initialization"


	''' <summary>
	''' Constructs a new menu entry with the specified text.
	''' </summary>
	Public Sub New(text As String)
		Me.m_text = text
	End Sub


	#End Region

	#Region "Update and Draw"


	''' <summary>
	''' Updates the menu entry.
	''' </summary>
	Public Overridable Sub Update(screen As MenuScreen, isSelected As Boolean, gameTime As GameTime)
		' there is no such thing as a selected item on Windows Phone, so we always
		' force isSelected to be false
		#If WINDOWS_PHONE Then
		isSelected = False
		#End If

		' When the menu selection changes, entries gradually fade between
		' their selected and deselected appearance, rather than instantly
		' popping to the new state.
		Dim fadeSpeed As Single = CSng(gameTime.ElapsedGameTime.TotalSeconds) * 4

		If isSelected Then
			selectionFade = Math.Min(selectionFade + fadeSpeed, 1)
		Else
			selectionFade = Math.Max(selectionFade - fadeSpeed, 0)
		End If
	End Sub


	''' <summary>
	''' Draws the menu entry. This can be overridden to customize the appearance.
	''' </summary>
	Public Overridable Sub Draw(screen As MenuScreen, isSelected As Boolean, gameTime As GameTime)
		' there is no such thing as a selected item on Windows Phone, so we always
		' force isSelected to be false
		#If WINDOWS_PHONE Then
		isSelected = False
		#End If

		' Draw the selected entry in yellow, otherwise white.
		Dim color__1 As Color = If(isSelected, Color.Yellow, Color.White)

		' Pulsate the size of the selected menu entry.
		Dim time As Double = gameTime.TotalGameTime.TotalSeconds

		Dim pulsate As Single = CSng(Math.Sin(time * 6)) + 1

		Dim scale As Single = 1 + pulsate * 0.05F * selectionFade

		' Modify the alpha to fade text out during transitions.
		color__1 *= screen.TransitionAlpha

		' Draw text, centered on the middle of each line.
		Dim screenManager As ScreenManager = screen.ScreenManager
		Dim spriteBatch As SpriteBatch = screenManager.SpriteBatch
		Dim font As SpriteFont = screenManager.Font
		Dim origin As New Vector2(0, font.LineSpacing \ 2)

		spriteBatch.DrawString(font, m_text, m_position, color__1, 0, origin, _
			scale, SpriteEffects.None, 0)
	End Sub


	''' <summary>
	''' Queries how much space this menu entry requires.
	''' </summary>
	Public Overridable Function GetHeight(screen As MenuScreen) As Integer
		Return screen.ScreenManager.Font.LineSpacing
	End Function


	''' <summary>
	''' Queries how wide the entry is, used for centering on the screen.
	''' </summary>
	Public Overridable Function GetWidth(screen As MenuScreen) As Integer
		Return CInt(Math.Truncate(screen.ScreenManager.Font.MeasureString(Text).X))
	End Function


	#End Region
End Class
